<?php
namespace Admin\Controller;
use Admin\Controller\ChearkLoginController;
use Think\Controller;
use Think\Upload;

class ShopController extends ChearkLoginController {
    public function index(){
        $this->types = M('types')->select();
        $this->shop = M('shop')->join('types on shop.types_id = types.idty')->select();
        $this->display();
    }

    public function update(){
        import('ORG.Net.UploadFile');
       $types = $_REQUEST['id'];
       $shopname = $_REQUEST['shopname'];
       $img = $_FILES['shopimg'];
       $price = $_REQUEST['price'];
       $number =  $_REQUEST['number'];


       $uplod = new Upload();//实例化上传类
       $uplod->maxSize = 314728;//设置附件上传大小
       $uplod->exts =array('jpg','gif','png','jpeg');//设置附件上传类型
       $uplod->rootPath = './Public/imgs';//设置附件上传跟目录
       $uplod->savePath = '/';//设置附件上传目录
        //上传文件
        $info = $uplod->upload();
        $tu = $info["shopimg"]["savepath"].'/'.$info["shopimg"]["savename"];

        if(!$info){//上传错误提示信息
            echo "error";//上传错误
        }else{
            echo "success";//上传成功
        }

        $model = M('shop');
        $data['shopname'] = $shopname;
        $data['price'] = $price;
        $data['discount'] =1;
        $data['types_id'] = $types;
        $data['number'] = $number;
        $data['shopimg'] = $tu;

        $res = $model->data($data)->add();
        if(!empty($res)){
            $this->success('成功！');
        }else{
            $this->error('失败！');
        }
    }

    public function teijia(){
        $types = $_REQUEST['ids'];
        $shopname = $_REQUEST['shopname'];
        $price = $_REQUEST['price'];
        $number =  $_REQUEST['number'];
        $discount =  $_REQUEST['discount'];

        $uplod = new Upload();//实例化上传类
        $uplod->maxSize = 314728;//设置附件上传大小
        $uplod->exts =array('jpg','gif','png','jpeg');//设置附件上传类型
        $uplod->rootPath = './Public/imgs';//设置附件上传跟目录
        $uplod->savePath = '/';//设置附件上传目录
        //上传文件
        $info = $uplod->upload();
        $tu = $info["shopimg"]["savepath"].'/'.$info["shopimg"]["savename"];

        if(!$info){//上传错误提示信息
            echo "error";//上传错误
        }else{
            echo "success";//上传成功
        }

        $model = M('shop');
        $data['shopname'] = $shopname;
        $data['price'] = $price;
        $data['discount'] =$discount;
        $data['types_id'] = $types;
        $data['number'] = $number;
        $data['shopimg'] = $tu;
        $res = $model->data($data)->add();
        if(!empty($res)){
            $this->success('成功！');
        }else{
            $this->error('失败！');
        }
    }

    public function xiugai(){
        $dis = $_REQUEST['name'];
        $id = $_REQUEST['id'];
        $wher['idit'] = $id;
        $data['discount'] = $dis;
        $ree = M('shop')->where($wher)->data($data)->save();
        if(!empty($ree)){
            $this->success('修改成功！');
        }else{
            $this->error('修改失败！');
        }
    }

    public function csx(){
        $leixin = $_REQUEST['name'];
        $data['leibiename'] = $leixin;
        $red = M('types')->data($data)->add();
        if(!empty($red)){
            $this->success('新增成功！');
        }else{
            $this->error('新增失败！');
        }
    }
}